package com.dis.service.service.impl;

import com.dis.pojo.dto.MedicalCardDTO;
import com.dis.pojo.entity.Hospital;
import com.dis.pojo.entity.Insure;
import com.dis.pojo.entity.MedicalCard;
import com.dis.service.mapper.HospitalMapper;
import com.dis.service.mapper.InsureMapper;
import com.dis.service.mapper.MedicalCardMapper;
import com.dis.pojo.queryDTO.MedicalCardPageQueryDTO;
import com.dis.common.result.PageResult;
import com.dis.service.service.MedicalCardService;
import com.dis.pojo.vo.MedicalCardVO;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MedicalCardServiceImpl implements MedicalCardService {

    @Autowired
    private MedicalCardMapper medicalCardMapper;

    @Autowired
    private InsureMapper insureMapper;

    @Autowired
    private HospitalMapper hospitalMapper;

    @Override
    public void save(MedicalCardDTO medicalCardDTO) {
        MedicalCard medicalCard = new MedicalCard();
        BeanUtils.copyProperties(medicalCardDTO, medicalCard);
        medicalCardMapper.insert(medicalCard);
    }

    @Override
    public PageResult pageQuery(MedicalCardPageQueryDTO medicalCardPageQueryDTO) {
        PageHelper.startPage(medicalCardPageQueryDTO.getPage(), medicalCardPageQueryDTO.getPageSize());
        Page<MedicalCardVO> page = medicalCardMapper.pageQuery(medicalCardPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public MedicalCardVO getById(Long id) {
        MedicalCardVO medicalCardVO = medicalCardMapper.getById(id);
        if (medicalCardVO != null) {
            // 获取用户信息
            Insure insure = insureMapper.getById(medicalCardVO.getInsureId());
            if (insure != null) {
                medicalCardVO.setInsureName(insure.getInsureName());
            }

            // 获取医院信息
            if (medicalCardVO.getHospitalId() != null) {
                Hospital hospital = hospitalMapper.getById(medicalCardVO.getHospitalId().longValue());
                if (hospital != null) {
                    medicalCardVO.setHospitalName(hospital.getName());
                }
            }
        }
        return medicalCardVO;
    }

    @Override
    public void update(MedicalCardDTO medicalCardDTO) {
        MedicalCard medicalCard = new MedicalCard();
        BeanUtils.copyProperties(medicalCardDTO, medicalCard);
        medicalCardMapper.update(medicalCard);
    }

    @Override
    public void deleteById(Long id) {
        medicalCardMapper.deleteById(id);
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        MedicalCard medicalCard = new MedicalCard();
        medicalCard.setId(id);
        medicalCard.setStatus(status);
        medicalCardMapper.updateStatus(medicalCard);
    }
    /**
     * 根据病人ID查询病人的所有医疗卡
     */
    @Override
    public List<MedicalCardVO> getByInsureId(Long insureId) {
        return medicalCardMapper.getByInsureId(insureId);
    }
}